package com.dinadi.yidi.ui.activity;

import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.dinadi.yidi.R;
import com.dinadi.yidi.adapter.IntegralCenterAdapter;
import com.dinadi.yidi.base.BaseActivity;
import com.dinadi.yidi.bean.CreativeProductBean;
import com.dinadi.yidi.bean.CreativeProductRequest;
import com.dinadi.yidi.bean.CreativeProductRequestEntity;
import com.dinadi.yidi.bean.IntegralBean;
import com.dinadi.yidi.http.HttpListener;
import com.dinadi.yidi.http.interfaces.CommonInterface;
import com.dinadi.yidi.utils.JsonUtil;
import com.dinadi.yidi.utils.SharePreferencesHelper;
import com.dinadi.yidi.utils.ToastUtil;
import com.dinadi.yidi.utils.pullloadmorerecyclerview.PullLoadMoreRecyclerView;

import java.util.ArrayList;
import java.util.List;

/**
 * author: JiaYu.He
 * Date: 2017/11/30  积分中心
 * Time: 16:29
 */
public class IntegralCenterActivity extends BaseActivity {
    private View view;
    private TextView tvTitle, tvRight, tv_integral;
    private ImageView img_no_comment, img_sign;
    private RelativeLayout layout_no_comment;
    private PullLoadMoreRecyclerView mPullLoadMoreRecyclerView;
    private IntegralCenterAdapter mRefreshAdapter;
    private List<CreativeProductRequestEntity> dataList = new ArrayList<>();
    private String userId, token;
    private int page = 1;
    /*是否显示没有的图片*/
    private boolean isShow = true;
    private String status;
    private int a;


    @Override
    protected View getLayout() {
        view = LayoutInflater.from(IntegralCenterActivity.this).inflate(R.layout.activity_integral_center, null);
        return view;
    }

    @Override
    protected void noWifiRefresh() {

    }

    @Override
    protected void initView() {
        changePager(2);
        tvTitle = (TextView) view.findViewById(R.id.text_title);
        tvTitle.setText(R.string.text_integral_center);
        tvRight = (TextView) view.findViewById(R.id.text_title_right);
        tvRight.setText(R.string.text_record);
        tvRight.setVisibility(View.GONE);
        img_no_comment = (ImageView) view.findViewById(R.id.img_no_comment);
        mPullLoadMoreRecyclerView = (PullLoadMoreRecyclerView) view.findViewById(R.id.pullLoadMoreRecyclerView);
        mPullLoadMoreRecyclerView.setGridLayout(2);
        mRefreshAdapter = new IntegralCenterAdapter(IntegralCenterActivity.this);
        mPullLoadMoreRecyclerView.setAdapter(mRefreshAdapter);
        mPullLoadMoreRecyclerView.setOnPullLoadMoreListener(new PullLoadMoreListener());
        // 不需要下拉刷新
        mPullLoadMoreRecyclerView.setPullRefreshEnable(false);
        layout_no_comment = (RelativeLayout) view.findViewById(R.id.layout_no_comment);
        img_sign = (ImageView) view.findViewById(R.id.img_sign);
        tv_integral = (TextView) view.findViewById(R.id.tv_integral);


    }

    @Override
    protected void initListener() {
        registerClickListener(tvRight, img_sign);

    }

    @Override
    public void onResume() {
        super.onResume();

        String isRefresh = SharePreferencesHelper.getInstance(this).getString("isRefresh");
        if ("yes".equals(isRefresh)) {
            userId = SharePreferencesHelper.getInstance(IntegralCenterActivity.this).getString("userId");
            token = SharePreferencesHelper.getInstance(IntegralCenterActivity.this).getString("token");
            SharePreferencesHelper.getInstance(this).putString("isRefresh", "no");
        }
    }

    @Override
    protected void initData() {
        userId = SharePreferencesHelper.getInstance(IntegralCenterActivity.this).getString("userId");
        token = SharePreferencesHelper.getInstance(IntegralCenterActivity.this).getString("token");
        showLoadingDialog("");
        showIntegral();


    }


    @Override
    protected void clickEvent(View v) {
        switch (v.getId()) {
            case R.id.text_title_right:

                ToastUtil.showToast("记录");
                break;
            case R.id.img_sign:
                if (status.equals("1")) {
                    /*未签到*/
                    Sign();
                } else {
                    /*已签到*/
//                    Sign();
                    ToastUtil.showToast("今日已签到~");
                }
                break;
            default:
                break;
        }

    }

    /*签到*/
    private void Sign() {
        CreativeProductBean bean = new CreativeProductBean();
        bean.setToken(token);
        bean.setUserId(userId);
        CommonInterface creativeProductInterface = new CommonInterface(IntegralCenterActivity.this, SignHttpListener);
        creativeProductInterface.getInterface("11013", JSON.toJSONString(bean));
    }

    /*查看用户积分*/
    private void showIntegral() {
        CreativeProductBean bean = new CreativeProductBean();
        bean.setToken(token);
        bean.setUserId(userId);
        CommonInterface creativeProductInterface = new CommonInterface(IntegralCenterActivity.this, showIntegralListener);
        creativeProductInterface.getInterface("16041", JSON.toJSONString(bean));
    }

    /*是否签到*/
    private void ifSign() {
        CreativeProductBean bean = new CreativeProductBean();
        bean.setToken(token);
        bean.setUserId(userId);
        CommonInterface creativeProductInterface = new CommonInterface(IntegralCenterActivity.this, ifSignHttpListener);
        creativeProductInterface.getInterface("11012", JSON.toJSONString(bean));
    }

    /*积分增减*/
    private void IntegralChange() {
        IntegralBean bean = new IntegralBean();
        bean.setUserId(userId);
        bean.setToken(token);
        bean.setType("6");
        /*"增减 1-增 2-减",*/
        bean.setRegulation("1");
        CommonInterface creativeProductInterface = new CommonInterface(IntegralCenterActivity.this, IntegralChangeHttpListener);
        creativeProductInterface.getInterface("16040", JSON.toJSONString(bean));
    }

    /*查用户积分*/
    HttpListener showIntegralListener = new HttpListener() {

        @Override
        public void OnSuccess(String response) {
            super.OnSuccess(response);
            closeLoadingDialog();
            String errCode = JsonUtil.getFieldValue(response, "errCode");
            String errMsg = JsonUtil.getFieldValue(response, "errMsg");
            if (errCode.equals("0000")) {
                closeLoadingDialog();
                String pointNow = JsonUtil.getFieldValue(response, "point");
                tv_integral.setText(pointNow);
                ifSign();
            } else {
                closeLoadingDialog();
                toast(errMsg);
            }


        }

        @Override
        public void OnFail(String errorResponse) {
            super.OnFail(errorResponse);
            changePager(1);
            closeLoadingDialog();
            toast("加载失败");
        }
    };
    HttpListener httpListener = new HttpListener() {

        @Override
        public void OnSuccess(String response) {
            super.OnSuccess(response);
            closeLoadingDialog();
            String errCode = JsonUtil.getFieldValue(response, "errCode");
            String errMsg = JsonUtil.getFieldValue(response, "errMsg");
            if (errCode.equals("0000")) {
                closeLoadingDialog();
                CreativeProductRequest bean = JsonUtil.parseJsonToBean(response, CreativeProductRequest.class);
                dataList = bean.getProductList();
                if (dataList.size() == 0) {
                    if (isShow == false) {
                        layout_no_comment.setVisibility(View.GONE);
                    } else {
                        layout_no_comment.setVisibility(View.VISIBLE);
                        img_no_comment.setImageResource(R.drawable.icon_good_manners);

                    }
                } else {
                    layout_no_comment.setVisibility(View.GONE);
                }
                mRefreshAdapter.addAllData(dataList);
                getData();


            } else {
                closeLoadingDialog();
                toast(errMsg);
            }


        }

        @Override
        public void OnFail(String errorResponse) {
            super.OnFail(errorResponse);
            changePager(1);
            closeLoadingDialog();
            toast("加载失败");
        }
    };
    /*是否签到*/
    HttpListener ifSignHttpListener = new HttpListener() {

        @Override
        public void OnSuccess(String response) {
            super.OnSuccess(response);
            closeLoadingDialog();
            String errCode = JsonUtil.getFieldValue(response, "errCode");
            String errMsg = JsonUtil.getFieldValue(response, "errMsg");
            if (errCode.equals("0000")) {
                closeLoadingDialog();
                status = JsonUtil.getFieldValue(response, "status");
                /*1、未签到  2、签到*/
                if (status.equals("1")) {
                    img_sign.setImageResource(R.drawable.icon_integral_already);
                } else {
                    img_sign.setImageResource(R.drawable.icon_integral_no);
                }
                initPullrefresh();
            } else {
                closeLoadingDialog();
                toast(errMsg);
            }


        }

        @Override
        public void OnFail(String errorResponse) {
            super.OnFail(errorResponse);
            changePager(1);
            closeLoadingDialog();
            toast("加载失败");
        }
    };
    /*签到*/
    HttpListener SignHttpListener = new HttpListener() {

        @Override
        public void OnSuccess(String response) {
            super.OnSuccess(response);
            closeLoadingDialog();
            String errCode = JsonUtil.getFieldValue(response, "errCode");
            String errMsg = JsonUtil.getFieldValue(response, "errMsg");
            if (errCode.equals("0000")) {
                closeLoadingDialog();
                status = "2";
                img_sign.setImageResource(R.drawable.icon_integral_no);
                /*积分增减*/
                IntegralChange();
            } else {
                closeLoadingDialog();
                toast(errMsg);
            }


        }

        @Override
        public void OnFail(String errorResponse) {
            super.OnFail(errorResponse);
            changePager(1);
            closeLoadingDialog();
            toast("加载失败");
        }
    };
    /*积分增减*/
    HttpListener IntegralChangeHttpListener = new HttpListener() {

        @Override
        public void OnSuccess(String response) {
            super.OnSuccess(response);
            closeLoadingDialog();
            String errCode = JsonUtil.getFieldValue(response, "errCode");
            String errMsg = JsonUtil.getFieldValue(response, "errMsg");
            if (errCode.equals("0000")) {
                closeLoadingDialog();

                //"增减积分 增为10 减为-10",
                String point = JsonUtil.getFieldValue(response, "point");
                String status = JsonUtil.getFieldValue(response, "status");
                String toast = "积分+" + point + "\n签到成功";
                ToastUtil.showToast(toast);
                a = Integer.parseInt(tv_integral.getText().toString()) + Integer.parseInt(point);
                tv_integral.setText(a + "");
            } else {
                closeLoadingDialog();
                toast(errMsg);
            }


        }

        @Override
        public void OnFail(String errorResponse) {
            super.OnFail(errorResponse);
            changePager(1);
            closeLoadingDialog();
            toast("加载失败");
        }
    };

    private void getData() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {

                //刷新结束
                mPullLoadMoreRecyclerView.setPullLoadMoreCompleted();
            }
        }, 1000);
    }


    class PullLoadMoreListener implements PullLoadMoreRecyclerView.PullLoadMoreListener {
        @Override
        public void onRefresh() {
            setRefresh();
        }

        @Override
        public void onLoadMore() {
            page = page + 1;
            isShow = false;
            initPullrefresh();
        }
    }

    private void setRefresh() {
        mRefreshAdapter.clearData();
        page = 1;
        initPullrefresh();
    }

    /*积分商城列表*/
    private void initPullrefresh() {
        CreativeProductBean bean = new CreativeProductBean();
        bean.setToken(token);
        bean.setPageNum(page + "");
        bean.setPageSize("10");
        CommonInterface creativeProductInterface = new CommonInterface(IntegralCenterActivity.this, httpListener);
        creativeProductInterface.getInterface("16042", JSON.toJSONString(bean));

    }

}
